<?php
/**
 * Created by PhpStorm.
 * User: 李闯
 * Date: 2020/7/2
 * Time: 10:36
 * Content: 应用管理
 */

namespace app\admin\controller;


use app\reuse\controller\ResponseJson;
use app\service\ResourceService;
use Redis\Redis;
use think\Db;

class Apply extends Common
{
    use ResponseJson;

    function __construct()
    {
        parent::__construct();
        // 登录校验
        $this->IsLogin();
    }

    /*
     * @param null
     * @content 问题列表
     * @return array
     * */
    function qanswer_index()
    {
        if (input("get.active") == "ajax") {

            # 定义listRows
            $listRows = input('post.limit');
            if (empty($listRows)) {
                $listRows = 15;
            }
            $info = Db::table('qanswer q')
                ->field('q.id,q.question,q.answer,qt.qtype_title,qt.qtype_imgurl')
                ->join("qtype qt", "qt.id = q.qtype_id")
                ->paginate($listRows, false, ['query' => request()->param()]);
            # 返回数据
            return $this->jsonSuccessData($info);
        } else {
            return $this->fetch();

        }

    }

    /*
     * @param null
     * @content 问题创建
     * @return array
     * */
    function qanswer_add()
    {
        if (input("get.active") == "ajax") {
            $info = input("post.");
            unset($info['file']);
            if (!empty($info)) {
                #执行添加操作
                $result = Db::table("qanswer")->insert($info);
                if ($result) {
                    # 返回数据
//                    return $this->jsonSuccessData('',true);
                    $this->changeHelpCenterRedis();
                    return array("status" => true, 'msg' => '提交成功');

                } else {
                    return array("status" => false, 'msg' => '提交失败');
                }
            } else {
                return array("status" => false, 'msg' => '没有接收到参数');

            }

        } else {
            #查询类型
            $qtype = Db::table("qtype")->select();
            $this->assign("qtype", $qtype);
            return $this->fetch();
        }

    }

    /*
     * @param null
     * @content 问题修改
     * @return array
     * */
    function qanswer_write()
    {
        if (input("get.active") == "ajax") {
            $id = input("post.id");
            $info = input("post.");
            unset($info['file']);
            if (!empty($info)) {
                #执行添加操作
                $result = Db::table("qanswer")->where("id = $id")->update($info);
                if ($result) {
                    # 返回数据
//                    return $this->jsonSuccessData('',true);
                    $this->changeHelpCenterRedis();
                    return array("status" => true, 'msg' => '提交成功');

                } else {
                    return array("status" => false, 'msg' => '提交失败');
                }
            } else {
                return array("status" => false, 'msg' => '没有接收到参数');

            }

        } else {
            $id = input("get.id");
            #查询类型
            $qtype = Db::table("qtype")->select();
            #查询问题
            $qanswer = Db::table("qanswer")->where("id= $id")->find();
            $this->assign("id", $id);
            $this->assign("qtype", $qtype);
            $this->assign("qanswer", $qanswer);
            return $this->fetch();
        }

    }

    function changeHelpCenterRedis()
    {
        $redis = new Redis();
        $info = Db::table('qtype')->order('id desc')->select();
        if (!empty($info)) {
            foreach ($info as $k => $v) {
                $info[$k]['icon'] = imgUrl($v['qtype_imgurl']);
                $list = Db::table('qanswer')->where(array('qtype_id' => $v['id']))->select();
                if (!empty($list)) {
                    foreach ($list as $lk=>$lv){
                        $list[$lk]['answer'] = ResourceService::ContentStaticReplace($lv['answer']);
                    }
                    $info[$k]['list'] = $list;
                } else {
                    unset($info[$k]);
                }
            }
            $redis->hSetJson('HelpCenter', 'List', $info);
        }
    }
    /*************************************************类型*******************************************************/
    /*
    * @param null
    * @content 问题类型列表
    * @return array
    * */
    function qtype_index()
    {
        if (input("get.active") == "ajax") {
            # 定义listRows
            $listRows = input('post.limit');
            if (empty($listRows)) {
                $listRows = 15;
            }
            $info = Db::table('qtype')
                ->paginate($listRows, false, ['query' => request()->param()]);
            # 返回数据
            return $this->jsonSuccessData($info);
        } else {
            $http = $_SERVER["REQUEST_SCHEME"] . '://' . $_SERVER["HTTP_HOST"] . '/';
            $this->assign('http', $http);
            return $this->fetch();
        }
    }

    /*
       * @param null
       * @content 问题类型创建
       * @return array
       * */
    function qtype_add()
    {
        if (input("get.active") == "ajax") {
            $info = input("post.");
            unset($info['imgurl']);
            if (!empty($info)) {
                #执行添加操作
                $result = Db::table("qtype")->insertGetId($info);
                if ($result) {
                    #创建成功
                    $this->changeHelpCenterRedis();
                    return array("status" => true, 'msg' => '提交成功');

                } else {
                    return array("status" => false, 'msg' => '提交失败');
                }
            } else {
                return array("status" => false, 'msg' => '没有接收到参数');

            }

        } else {
            #查询类型
            return $this->fetch();
        }

    }

    /*
  * @param null
  * @content 问题类型修改
  * @return array
  * */
    function qtype_write()
    {
        if (input("get.active") == "ajax") {
            $info = input("post.");
            $id = input("post.id");
            if (!empty($info)) {
                #判断是否有新上传图片  如果有 把旧的删除
                if (!empty($info['oldimgurl'])) {
                    #有新上传的
                    #比较 旧图片 和新图片 是否一样
                    $info['qtype_imgurl'] = 1;
                }
                unset($info['imgurl'], $info['oldimgurl']);

                #执行添加操作
                $result = Db::table("qtype")->where("id = $id")->update($info);
                if ($result) {
                    # 返回数据
//                    return $this->jsonSuccessData('',true);
                    $this->changeHelpCenterRedis();
                    return array("status" => true, 'msg' => '提交成功');

                } else {
                    return array("status" => false, 'msg' => '提交失败');
                }
            } else {
                return array("status" => false, 'msg' => '没有接收到参数');

            }

        } else {
            $id = input("get.id");
            #查询类型
            #查询问题
            $qtype = Db::table("qtype")->where("id= $id")->find();
            $this->assign("qtype", $qtype);
            $this->assign("id", $id);
            return $this->fetch();
        }

    }
    /*************************************************规则 协议 等*******************************************************/
    /*
    * @param null
    * @content 规则列表
    * @return array
    * */
    function rules_index()
    {
        if (input("get.active") == "ajax") {
            $_where = "";
            $rules_type = input("get.rules_type");
            if (!empty($rules_type)) {
                $_where .= " rules_type = $rules_type";
            } else {
                $type = input("get.type");
                if (!empty($type)) {
                    $_where .= "rules_type = 0 and type = $type";

                }
            }
            # 定义listRows
            $listRows = input('post.limit');
            if (empty($listRows)) {
                $listRows = 10;
            }
            $info = Db::table("protocol")
                ->where($_where)
                ->paginate($listRows, false, ['query' => request()->param()]);
            # 返回数据
            return $this->jsonSuccessData($info);
        } else {
            $rules_type = input("get.rules_type");
            $type = input("get.type");
            $this->assign("rules_type", $rules_type);
            $this->assign("type", $type);
            return $this->fetch();
        }
    }

    /*
     * @param null
     * @content 规则创建
     * @return array
     * */
    function rules_add()
    {
        if (input("get.active") == "ajax") {
            $info = input("post.");
            $info['rules_type'] = 1;
            if (!empty($info)) {
                #查询是否有相同类型
                $type = Db::table("protocol")->where("type = '" . $info['type'] . "'")->find();
                if (empty($type)) {
                    #执行添加操作
                    $result = Db::table("protocol")->insert($info);
                    if ($result) {
                        # 返回数据
                        return array("status" => true, 'msg' => '提交成功');

                    } else {
                        return array("status" => false, 'msg' => '提交失败');
                    }
                } else {
                    #有的话  提示  只能添加一个
                    return array("status" => false, 'msg' => '每个类型只能创建一个,请换一个创建');
                }
            } else {
                return array("status" => false, 'msg' => '没有接收到参数');

            }

        } else {
            #查询类型
            return $this->fetch();
        }

    }

    /*
     * @param null
     * @content 问题类型修改
     * @return array
     * */
    function rules_write()
    {
        if (input("get.active") == "ajax") {
            $info = input("post.");
            $id = input("post.id");

            if (!empty($info)) {
                #查询是否有相同类型
                $type = Db::table("protocol")->where("type = '" . $info['type'] . "' and id!=$id")->find();
                if (empty($type)) {
                    #执行添加操作
                    $result = Db::table("protocol")->where("id = $id")->update($info);
                    # 返回数据
                    return array("status" => true, 'msg' => '保存成功');
                } else {
                    return array("status" => false, 'msg' => '每个类型只有一个,请换一个保存!');
                }
            } else {
                return array("status" => false, 'msg' => '没有接收到参数');

            }

        } else {
            $id = input("get.id");
            #查询问题
            $rules = Db::table("protocol")->where("id= $id")->find();
            $this->assign("rules", $rules);
            $this->assign("id", $id);
            return $this->fetch();
        }

    }

    /*
     * @param null
     * @content Q/A删除
     * @return array
     * */
    function apply_del()
    {
        #删除的id
        $id = input("post.id");
        #删除标识
        $mark = input("post.mark");
        #旧图片
        $imgurl = input("post.imgurl");
        if (!empty($id)) {
            if ($mark == 'qanswer') {
                #问题删除
                Db::table('qanswer')->where("id = $id")->delete();

            } else if ($mark == 'qtype') {
                #获取根路径
                $ROOT_PATH = ROOT_PATH;
                $oldimgurl = $ROOT_PATH . "upload/" . $imgurl;
                ResourceService::delFile($oldimgurl);
                #问题类型删除
                Db::table('qtype')->where("id = $id")->delete();
            }
            return array("status" => true);
        }
    }

    /*
    * @param null
    * @content 查看内容
    * @return array
    * */
    function apply_content()
    {
        return array("status" => true);
    }

    /**
     * @return array|mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @context 版本修改
     */
    function version_write()
    {
        if(input("get.active")!='ajax'){
            $id=input("get.id");
            # 查询版本控制
            $info=Db::table("version_control")->where(array("id"=>$id))->find();
            $this->assign("info",$info);
            return $this->fetch();
        }else{
            $param=input("post.");
            $id=$param['id'];
            unset($param['id']);
            Db::table("version_control")->where(array("id"=>$id))->update($param);
            $_redis=new Redis();
            $_redis->hDel("version","control");
            return array("status" => true, 'msg' => '保存成功');
        }
    }

    /**
     * @return mixed|string
     * @throws \think\exception\DbException
     * @context 版本列表
     */
    function version_list()
    {
        if(input("get.active")!='ajax'){
            return $this->fetch();
        }else{
            $listRows = input('post.limit');
            if (empty($listRows)) {
                $listRows = 10;
            }
            $list=Db::table("version_control vc")
                 ->field("vc.*,if(vc.force_update=1,'强制','不强制') force_title")
                ->order("id desc")->paginate($listRows, false, ['query' => request()->param()]);
            # 返回数据
            return $this->jsonSuccessData($list);
        }
    }

    /**
     * @return array|mixed
     * @context 版本添加
     */
    function version_add()
    {
        if(input("get.active")!='ajax'){
            return $this->fetch();
        }else{
            $info=input("post.");
            Db::table("version_control")->insert($info);
            $_redis=new Redis();
            $_redis->hDel("version","control");
            return array("status" => true, 'msg' => '添加成功');
        }
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @context 版本删除
     */
    function version_delete()
    {
        $id=input("post.id");
        Db::table("version_control")->where(array("id"=>$id))->delete();
        $_redis=new Redis();
        $_redis->hDel("version","control");
        return array("status"=>true);
    }
}